<template>
    <div class="mail-template-config-container layout-padding">
        <el-card shadow="hover" class="layout-padding-auto">
            <el-card shadow="hover">
                <div class="bar_btn_div">
                    <div class="title_div">
                        <div class="el-icon-tickets"></div>
                        <div>筛选</div>
                    </div>
                    <div>
                        <el-button size="default" @click.native.prevent="handleResetSearch()">重置搜索</el-button>
                        <el-button type="primary" size="default" @click.native.prevent="refresh()">查询搜索</el-button>


                    </div>
                </div>

                <div class="search_div">
                    <el-form :model="state.ruleForm" label-width="120px">
                        <div style="display: flex; flex-wrap: wrap">
                            <el-form-item label="模板编码">
                                <el-input clearable v-model="state.ruleForm.templateCode" class="input-width" placeholder="模板编码"></el-input>
                            </el-form-item>

                            <el-form-item label="模板内容">
                                <el-input clearable v-model="state.ruleForm.templateText" class="input-width" placeholder="模板内容"></el-input>
                            </el-form-item>

                            <el-form-item label="接收人">
                                <el-input clearable v-model="state.ruleForm.toAddress" class="input-width" placeholder="接收人（模糊查询）"></el-input>
                            </el-form-item>

                            <el-form-item label="主题">
                                <el-input clearable v-model="state.ruleForm.subject" class="input-width" placeholder="主题"></el-input>
                            </el-form-item>

                            <el-form-item label="语言">
                                <el-input clearable v-model="state.ruleForm.language" class="input-width" placeholder="语言"></el-input>
                            </el-form-item>

                        </div>
                    </el-form>
                </div>
            </el-card>



            <el-card class="box-card" style="margin-top: 20px;">
                <div class="bar_tools">
                    <el-button-group class="ml-4">
                        <el-button size="small" type="primary" @click="handlerAdd()">新增</el-button>
                    </el-button-group>
                </div>
                <el-table size="default" :data="state.dataList" style="border: 1px solid #ebeef5; margin-top: 20px">
                    <el-table-column align="center" label="模板编码" show-overflow-tooltip>
                        <template #default="scope">
                            <span>{{ scope.row.templateCode }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="语言">
                        <template #default="scope">
                            <span>{{ scope.row.language }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="发送人">
                        <template #default="scope">
                            <span>{{ scope.row.fromAddress }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="接收人" show-overflow-tooltip>
                        <template #default="scope">
                            <span>{{ scope.row.toAddress }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="主题">
                        <template #default="scope">
                            <span>{{ scope.row.subject }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="邮件抄送">
                        <template #default="scope">
                            <span>{{ scope.row.cc }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="邮件密送">
                        <template #default="scope">
                            <span>{{ scope.row.bcc }}</span>
                        </template>
                    </el-table-column>


                    <el-table-column align="center" label="创建时间" width="160">
                        <template #default="scope">
                            <span>{{ formatDate(new Date(scope.row.createdDate), "YYYY-mm-dd HH:MM:SS") }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="更新时间" width="160">
                        <template #default="scope">
                            <span>{{ formatDate(new Date(scope.row.createdDate), "YYYY-mm-dd HH:MM:SS") }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="操作" width="80px" fixed="right">
                        <template #default="scope">
                            <el-button @click="handlerEdit(scope.$index, scope.row)" type="primary" size="small">编辑</el-button><br>
                        </template>
                    </el-table-column>
                </el-table>

                <div class="pagination_div">
                    <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15" :pager-count="5"
                        :page-sizes="[10, 20, 30]" v-model:current-page="state.ruleForm.page" background v-model:page-size="state.ruleForm.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="state.ruleForm.total">
                    </el-pagination>
                </div>

            </el-card>

        </el-card>

        <editdialog ref="editdialogRef" @refresh="refresh" />

    </div>
</template>


<script setup lang="ts" name="mailsendconfig">

import { formatDate } from '../../../utils/formatTime';
import { reactive, onMounted, ref, defineAsyncComponent } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
// 导入接口
import { useMailApi } from "../../../api/mail";

// 引入组件
const editdialog = defineAsyncComponent(() => import('./editdialog.vue'))
const editdialogRef = ref();

const state = reactive({
    dataList: [] as any,
    selectUserName: [],
    ruleForm: {
        page: 1,
        limit: 10,
        total: 0,
        templateCode: "",
        templateText: "",
        toAddress: "",
        subject: "",
        language: "",

    },



});
//新增按钮
const handlerAdd = () => {
    editdialogRef.value.openDialog();
}
//编辑按钮
const handlerEdit = (index: any, row: any) => {
    editdialogRef.value.openDialog("edit", row);
}


const handleResetSearch = () => {
    state.ruleForm = {
        page: 1,
        limit: 10,
        total: 0,
        templateCode: "",
        templateText: "",
        toAddress: "",
        subject: "",
        language: "",
    };
    queryMailboxTemplate();
}

const refresh = () => {
    queryMailboxTemplate();
}


// 分页改变
const onHandleSizeChange = (val: number) => {
    state.ruleForm.limit = val;
    queryMailboxTemplate();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
    state.ruleForm.page = val;
    queryMailboxTemplate();
};
const queryMailboxTemplate = async () => {
    try {
        let vo = {
            limit: state.ruleForm.limit,
            page: state.ruleForm.page
        } as any;

        if (state.ruleForm.templateCode) vo["templateCode"] = state.ruleForm.templateCode;
        if (state.ruleForm.templateText) vo["templateText"] = state.ruleForm.templateText;
        if (state.ruleForm.toAddress) vo["toAddress"] = state.ruleForm.toAddress;
        if (state.ruleForm.subject) vo["subject"] = state.ruleForm.subject;
        if (state.ruleForm.language) vo["language"] = state.ruleForm.language

        let tempList = await useMailApi().queryMailboxTemplate(vo);
        state.dataList = tempList.list;
        state.ruleForm.total = tempList.total;

    } catch (error: any) {
        ElMessage.error(error.msg);
    }
}

// 页面加载时
onMounted(async () => {
    await queryMailboxTemplate();
});

</script>



<style lang="scss" scoped>
.el-form .el-form-item:last-of-type {
    margin-bottom: 22px !important;
}

.bar_tools {
    display: flex;
    flex-direction: row-reverse;
    margin-bottom: 10px;
}

.mail-template-config-container {
    :deep(.el-card__body) {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;

        .el-table {
            flex: 1;
        }
    }
}

.bar_btn_div {
    display: flex;
    padding: 20px;
    border: 1px solid #ebeef5;
    align-items: center;
    justify-content: space-between;
    border-radius: 5px 5px 0px 0px;

    .title_div {
        display: flex;
        font-size: 16px;

        &>div {
            margin-right: 10px;
        }
    }
}

.search_div {
    border: 1px solid #ebeef5;
    border-top: 0px;
    border-radius: 0px 0px 5px 5px;
    padding: 20px;
    padding-bottom: 0px;
}
</style>

